package cn.uikit.demo.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import sivin.uikit.utils.dp
import kotlin.math.sqrt

class KtProgressView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val cornerRadius = 16f.dp * 1.0f // 圆角半径

    private var progress = 0f // 当前进度 (0 到 100)

    private val backgroundPaint = Paint().apply {
        color = 0xFFFF5D2F.toInt() //
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    private val progressPaint = Paint().apply {
        color = 0xFF3880F2.toInt() // 进度颜色 (蓝绿色)
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    private val borderPaint = Paint().apply {
        color = 0xFFFFFFFF.toInt() // 边框颜色 (白色)
        style = Paint.Style.STROKE
        strokeWidth = 2f.dp * 1.0f // 边框宽度
        isAntiAlias = true
    }

    fun setProgress(value: Float) {
        progress = value.coerceIn(0f, 100f) // 确保进度值在 0 到 100 之间
        invalidate() // 请求重新绘制
    }

    private val rectF = RectF(0f, 0f, 0f, 0f)

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val width = width.toFloat()
        val height = height.toFloat()
        // 绘制背景圆角矩形
        rectF.set(0f, 0f, width, height)
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, backgroundPaint)
        // 绘制进度圆角矩形
        if (progress > 0) {
            val progressWidth = width * (progress / 100)
            val radius = height / 2f
            if (progressWidth <= radius) {
                val sweepAngle = 180f // 根据进度计算扇形角度
                val chordHeight = calcChord(radius, radius - progressWidth)
                rectF.set(
                    0f,
                    (height - chordHeight) / 2f,
                    chordHeight,
                    (height - chordHeight) / 2f + chordHeight
                )
                canvas.drawArc(rectF, 90f, sweepAngle, true, progressPaint)
            } else if (progressWidth > width - radius) {
                rectF.set(0f, 0f, progressWidth, height)
                canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, progressPaint)
            } else {
                val sweepAngle = 180f
                rectF.set(0f, 0f, height, height)
                canvas.drawArc(rectF, 90f, sweepAngle, true, progressPaint)
                canvas.drawRect(height / 2, 0f, progressWidth, height, progressPaint)
            }
        }
        // 绘制白色边框
        rectF.set(
            borderPaint.strokeWidth / 2,
            borderPaint.strokeWidth / 2,
            width - borderPaint.strokeWidth / 2,
            height - borderPaint.strokeWidth / 2
        )
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, borderPaint)
    }

    private fun calcChord(R: Float, d: Float): Float {
        if (R < d) return 0f
        return 2 * sqrt(R * R - d * d)
    }
}